package org.puppet.game.element;
import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Rect;


public class Animation {

    private ArrayList<AnimationFrame> frames;
    private int currFrameIndex;
    private long animTime;
    private long totalDuration;
    private Puppet puppet;
    private int actionNo;
    
    public int coreX=0;
    public int coreY=0;
    public int width=0;
    public int height=0;
    
    public Rect coreRect;
    
    protected boolean isLoop=true;
    
    public Animation(Puppet puppet) {
        frames = new ArrayList<AnimationFrame>();
        totalDuration = 0;
        this.puppet = puppet;
        reset();
    }
    
    public void reset() {
        animTime = 0;
        currFrameIndex = 0;
        refreshCurrentInfo();
        if (coreRect==null){
        	coreRect=new Rect(0, 0, width, height);
        }
    }
    
    public void refreshCurrentInfo(){
        if (frames.size() >0){
            coreX=getFrame(currFrameIndex).coreX;
            coreY=getFrame(currFrameIndex).coreY;
            width=getFrame(currFrameIndex).width;
            height=getFrame(currFrameIndex).height;
        }else{
        	coreX=0;
        	coreY=0;
        	width=0;
        	height=0;
        }
    }

    public void addFrame(Bitmap image,long duration) {
    	addFrame(image,duration, 0,0);
    }
    
    public void addFrame(Bitmap image,long duration,int x, int y) {
    	addFrame(new AnimationFrame(image, duration,x,y));
    }
    
    public void addFrame(AnimationFrame frame ){
        totalDuration += frame.duration;
    	frame.endTime=totalDuration;
        frames.add(frame);
        reset();
    }
    
    public void setFrames(AnimationFrame[] frames ){
    	for (AnimationFrame frame : frames){
    		addFrame(frame);
    	}
    	
    }
    public void setAllFrameCoreXY(int coreX, int coreY) {
    	for (AnimationFrame anim : frames){
    		anim.coreX=coreX;
    		anim.coreY=coreY;
    	}
    	refreshCurrentInfo();
    }

    public void update(long elapsedTime) {
        if (frames.size() > 1) {
            animTime += elapsedTime;
            if (animTime >= totalDuration) {
                animTime = animTime % totalDuration;
                currFrameIndex = 0;
                puppet.onAnimationEnd(this.actionNo);
            }
            while (animTime > getFrame(currFrameIndex).endTime) {
                currFrameIndex++;
            }
        }
        refreshCurrentInfo();
    }

    public Bitmap getImage() {
    	return frames.size() <1 ?null:getFrame(currFrameIndex).image;
    }

    private AnimationFrame getFrame(int i) {
        return frames.get(i);
    }

	public Rect getCoreRect() {
		return coreRect;
	}

	public void setCoreRect(Rect coreRect) {
		this.coreRect=coreRect;
	}
	
	public void setCoreRect(int left,int top,int width,int height) {
		this.coreRect=new Rect(left, top, left+width, top+height);
	}

	public Puppet getPuppet() {
		return puppet;
	}

	public void setPuppet(Puppet puppet) {
		this.puppet = puppet;
	}

	public int getActionNo() {
		return actionNo;
	}

	public void setActionNo(int actionNo) {
		this.actionNo = actionNo;
	}
	
}

